#include "DatetimeParam.h"

void CDatetimeParam::FromDate(const QDate& _date)
{
    usYear = (quint16) _date.year();
    uaMonth = (quint8) _date.month();
    uaDay = (quint8) _date.day();
}

void CDatetimeParam::FromTime(const QTime& _time)
{
    uaHour = (quint8) _time.hour();
    uaMin = (quint8) _time.minute();
    uaSec = (quint8) _time.second();
    usMsec = (quint16) _time.msec();
}

void CDatetimeParam::FromDatetime(const QDateTime& _datetime)
{
    usYear = (quint16) _datetime.date().year();
    uaMonth = (quint8) _datetime.date().month();
    uaDay = (quint8) _datetime.date().day();

    uaHour = (quint8) _datetime.time().hour();
    uaMin = (quint8) _datetime.time().minute();
    uaSec = (quint8) _datetime.time().second();
    usMsec = (quint16) _datetime.time().msec();
}

QDate CDatetimeParam::ToDate()
{
    return QDate((int)usYear, (int)uaMonth, (int)uaDay);
}

QTime CDatetimeParam::ToTime()
{
    return QTime((int)uaHour, (int)uaMin, (int)uaSec, (int)usMsec);
}

QDateTime CDatetimeParam::ToDatetime()
{
    return QDateTime(QDate((int)usYear, (int)uaMonth, (int)uaDay)
                     , QTime((int)uaHour, (int)uaMin, (int)uaSec, (int)usMsec));
}

//获取字符串值(格式为yyyy-MM-dd HH:mm:ss)
QString CDatetimeParam::GetDatetimeValue()
{
    return ToDatetime().toString("yyyy-MM-dd HH:mm:ss");
}

bool CDatetimeParam::IsValid()
{
    return
        usYear > 2017 && usYear < 3018
        && uaMonth >= 1 && uaMonth <= 12
        && uaDay >= 1 && uaDay <= 31
        && uaHour >= 0 && uaHour <= 23
        && uaMin >= 0 && uaMin <= 59
        && uaSec >= 0 && uaSec <= 59
        ;
}
